//
//  TicketHolderVM.swift
//  ANOA
//
//  Created by 邱仙凯 on 2019/6/25.
//  Copyright © 2019 zwb. All rights reserved.
//

import UIKit

class TicketHolderVM: NSObject {
    
    // MARK: - Getter And Setter
    var status = "未报销"
    var one_X:TicketModel?
    var fCompany = 0   // 0  个人  1  公司
    var invoiceTypeCode = "" // 1 为销售发票  其他为其他
    
    var idAname:idAndName?   // 公司的id和名称
    var dataArray = [TicketModel]()
    
    //    weak var org:UIButton?
    
    static func cropImage(_ inputImage: UIImage, toRect cropRect: CGRect ) -> UIImage?
       {
      
           // Perform cropping in Core Graphics
           guard let cutImageRef: CGImage = inputImage.cgImage?.cropping(to:cropRect)
           else {
               return nil
           }
           // Return image to UIImage
           let croppedImage: UIImage = UIImage(cgImage: cutImageRef)
           return croppedImage
       }
     
    
    
    /// 返回   数组个数，数组中 fTotalamoun t的累加
    func countSumInformation (_ arr:[TicketModel]) -> (String,String) {
        let sum = arr.compactMap { doubleTran($0.fTotalamount) }.reduce(0, +)
        let sumA = String(format:"%.2f",sum)
        return ("\(arr.count)",sumA)
    }
    
}

class AllSelectBtn:UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let width_r:CGFloat = 25
        // 设置title
     imageView?.frame = CGRect(x: 0 , y: (self.frame.height - width_r) * 0.5, width: width_r, height: width_r)
        // 设置imageView
        titleLabel?.frame = CGRect(x: width_r + 3 , y: (self.frame.height - width_r) * 0.5, width: self.frame.width - width_r - 3, height: width_r)
        
    }
}
 



struct OCRResult: SwiftyJSONAble {
    
    var orgWidth:Double?
    var orgHeight:Double?
    var subMsgs:[SubMsgs]?
    var errorMsg:String?

    
    init(object: Any) {
        self.init(json: JSON(object))
    }
    
    /// Initiates the instance based on the JSON that was passed.
    ///
    /// - parameter json: JSON object from SwiftyJSON.
    init(json: JSON) {
        if let items = json["subMsgs"].array { subMsgs = items.map { SubMsgs(json: $0) } }
        orgWidth = json["orgWidth"].double
        orgHeight = json["orgHeight"].double
        errorMsg = json["errorMsg"].string
    }
 
}
struct SubMsgs: SwiftyJSONAble {
    var sliceRect:SliceRect?
    var invoiceResult:InvoiceResult?
    var type:String?
    
    init(object: Any) {
        self.init(json: JSON(object))
    }
    
    /// Initiates the instance based on the JSON that was passed.
    ///
    /// - parameter json: JSON object from SwiftyJSON.
    init(json: JSON) {
        type = json["type"].string
      sliceRect = SliceRect(json: json["sliceRect"])
      invoiceResult = InvoiceResult(json: json["result"])
    }
 
}
struct InvoiceData: SwiftyJSONAble {
    var fInvoicecode:String? // 代码
    var fInvoiceno:String?  // 号码
    
    var capitalMoney:String?  // 大写
    var lowercaseMoney:String?  // 小写
    var invoiceMoney:String? // 发票金额
    var onlyMoney:String? // 金额  出租车发票有这样的返回
    
    var verifyCode:String? // 校验码
    var invoiceDate:String?
    var taxiDate:String?
    var typeForAddValue:String? // 增值税的具体类型    普通还是专用
    var moneyNoTax:String? // 金额(不含税)
    var trainMoney:String? // 火车票 票价
    var startStation:String?
    var endStation:String?
    var trainNo:String?
    var saler:String?
    var buyer:String?
    var buyerTaxno:String?
    var salerTaxno:String?
    var serviceName:String?
    var invoiceTitle:String?
    var invoiceDetail:[InvoiceDetail]?
    init(object: Any) {
        self.init(json: JSON(object))
    }
 
    /// Initiates the instance based on the JSON that was passed.
    ///
    /// - parameter json: JSON object from SwiftyJSON.
    init(json: JSON) {
       fInvoicecode = json["发票代码"].string
        fInvoiceno = json["发票号码"].string
        capitalMoney = json["大写金额"].string
        invoiceTitle = json["标题"].string

        lowercaseMoney = json["小写金额"].string
        invoiceMoney = json["发票金额"].string
        onlyMoney = json["金额"].string
        verifyCode = json["校验码"].string
        invoiceDate = json["开票日期"].string
        taxiDate = json["日期"].string
        typeForAddValue = json["发票类型"].string
         if json["金额(不含税)"].string.unwrap != "" {
             moneyNoTax = json["金额(不含税)"].string
        } else {
            moneyNoTax = json["不含税金额"].string
        }
       trainMoney = json["票价"].string
         startStation = json["出发站"].string
         endStation = json["到达站"].string
         trainNo = json["车次"].string
        saler = json["销售方名称"].string
        salerTaxno = json["销售方税号"].string
        buyer = json["受票方名称"].string
        buyerTaxno = json["受票方税号"].string
         if let items = json["发票详单"].array { invoiceDetail = items.map { InvoiceDetail(json: $0) } }
         
        
    }
 
}
struct InvoiceDetail: SwiftyJSONAble {
    var goodsName:String?
     
    
    init(object: Any) {
        self.init(json: JSON(object))
    }
    
    /// Initiates the instance based on the JSON that was passed.
    ///
    /// - parameter json: JSON object from SwiftyJSON.
    init(json: JSON) {
        goodsName = json["货物或应税劳务、服务名称"].string

    }
 
}

struct InvoiceResult: SwiftyJSONAble {
    var invoiceData:InvoiceData?
    
 
    
    init(object: Any) {
        self.init(json: JSON(object))
    }
    
    /// Initiates the instance based on the JSON that was passed.
    ///
    /// - parameter json: JSON object from SwiftyJSON.
    init(json: JSON) {
        invoiceData = InvoiceData(json: json["data"])

    }
 
}

struct SliceRect: SwiftyJSONAble {
    var x0:Double?
    var x1:Double?
    var x2:Double?
    var x3:Double?
    var y0:Double?
    var y1:Double?
    var y2:Double?
    var y3:Double?
    
    init(object: Any) {
        self.init(json: JSON(object))
    }
    
    /// Initiates the instance based on the JSON that was passed.
    ///
    /// - parameter json: JSON object from SwiftyJSON.
    init(json: JSON) {
       x0 = json["x0"].double
        x1 = json["x1"].double
        x2 = json["x2"].double
        x3 = json["x3"].double
        y0 = json["y0"].double
        y1 = json["y1"].double
        y2 = json["y2"].double
        y3 = json["y3"].double
    }
 
}
